package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysDept;
import com.farm.kaiping.po.SysDeptPO;
import com.farm.kaiping.service.SysDeptService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理(SysDept)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:51
 */
@RestController
@RequestMapping("/sysDept")
@AllArgsConstructor
@Api(tags = "部门管理控制器")
public class SysDeptController {

	private final SysDeptService sysDeptService;

	@ApiOperation(value = "分页查询部门管理", notes = "权限：view_sysDept_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_page')")
	@PostMapping("/page")
	public R<Page<SysDept>> getSysDeptPage(@RequestBody SysDeptPO sysDeptPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysDeptPO);
		return R.ok(sysDeptService.getSysDeptPage(page, sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询部门管理列表", notes = "权限：view_sysDept_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_list')")
	@PostMapping("/list")
	public R<List<SysDept>> getSysDeptList(@RequestBody SysDeptPO sysDeptPO) {
		try{
			return R.ok(sysDeptService.getSysDeptList(sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询部门管理", notes = "权限 view_sysDept_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysDeptService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条部门管理", notes = "权限：view_sysDept_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_one')")
	@PostMapping("/one")
	public R<SysDept> getSysDept(@RequestBody SysDeptPO sysDeptPO) {
		try{
			return R.ok(sysDeptService.getSysDept(sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增部门管理", notes = "权限：view_sysDept_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_add')")
	@PostMapping("/add")
	public R saveSysDept(@RequestBody SysDeptPO sysDeptPO) {
		try{
		    return R.ok(sysDeptService.saveSysDept(sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改部门管理", notes = "权限：view_sysDept_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_edit')")
	@PostMapping("/edit")
	public R updateSysDept(@RequestBody SysDeptPO sysDeptPO) {
		try{
		    return R.ok(sysDeptService.updateSysDept(sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除部门管理", notes = "权限：view_sysDept_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_del')")
	@PostMapping("/del")
	public R removeSysDept(@RequestBody SysDeptPO sysDeptPO) {
		try{
		    return R.ok(sysDeptService.removeSysDept(sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	@ApiOperation(value = "通过条件获取部门的树形结构列表", notes = "权限：view_sysDept_tree")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDept_tree')")
	@PostMapping("/tree")
	public R treeSysDept(@RequestBody SysDeptPO sysDeptPO) {
		try{
			return R.ok(sysDeptService.treeSysDept(sysDeptPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
}

